package com.union.core.workModel.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hnjowe.webdb.DBOperation;
import com.union.core.workModel.dao.ModelIncomDao;
import com.union.core.workModel.model.ModelIncom;

public class ModelIncomDaoImpl implements ModelIncomDao {
	private DBOperation opera;
	private ModelIncom modelIncom;
	private static Logger log = Logger.getLogger(ModelInfoDaoImpl.class.getName());
	@Override
	public boolean addIncom(ModelIncom modelIncom) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "insert into model_income(" +
					"model_id,income_year,income_sum,income_type) values " +
					"(?,?,?,?)";
			opera.prepStatment(sql);
			opera.setInt(1, modelIncom.getModel_id());
			opera.setString(2, modelIncom.getIncome_year());
			opera.setFloat(3, modelIncom.getIncome_sum());
			opera.setInt(4, modelIncom.getIncome_type());
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

	@Override
	public boolean delIncom(int incomId) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "delete from model_income where income_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, incomId);
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

	@Override
	public boolean editIncom(ModelIncom modelIncom) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "update model_income set income_year=?,income_sum=?," +
					"income_type=? where income_id=?";
			opera.prepStatment(sql);
			opera.setString(1, modelIncom.getIncome_year());
			opera.setFloat(2, modelIncom.getIncome_sum());
			opera.setInt(3, modelIncom.getIncome_type());
			opera.setInt(4, modelIncom.getIncome_id());
			opera.executeUpdate();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

	@Override
	public ModelIncom queryById(int incomId) {
		ModelIncom modelIncom = null;
		try {
			opera = new DBOperation();
			opera.transcationBegin();

			String sql = "select model_id,income_year,income_sum,income_type "
					+ "from model_income where  income_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, incomId);
			ResultSet res = opera.executeQuery();
			if (res.next()) {
				modelIncom = new ModelIncom();
				modelIncom.setIncome_id(incomId);
				modelIncom.setModel_id(res.getInt(1));
				modelIncom.setIncome_year(res.getString(2));
				modelIncom.setIncome_sum(res.getFloat(3));
				modelIncom.setIncome_type(res.getInt(4));
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return modelIncom;
	}

	@Override
	public List<ModelIncom> queryIncomList(int modelId, int type) {
		String sqlstr1 = ""; // 条件1
		String orderstr=" order by income_year asc";
		if(type!=0)sqlstr1=" and income_type="+type;
		List<ModelIncom> incomList = new ArrayList<ModelIncom>();
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "select "
					+ "income_id,model_id,income_year,income_sum,income_type "
					+ "from model_income "
					+ "where model_id=?"
					+ sqlstr1+ orderstr;
			opera.prepStatment(sql);
			opera.setInt(1, modelId);
			ResultSet res = opera.executeQuery();// 执行上面语句
			int i = 0;
			
			while (res.next()) {
				modelIncom = new ModelIncom();
				modelIncom.setIncome_id(res.getInt(1));
				modelIncom.setModel_id(res.getInt(2));
				modelIncom.setIncome_year(res.getString(3));
				modelIncom.setIncome_sum(res.getFloat(4));
				modelIncom.setIncome_type(res.getInt(5));
				incomList.add(i, modelIncom);
				modelIncom=null;
				i++;
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return incomList;
	}

	@Override
	public boolean delIncomList(int modelId) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "delete from model_income where model_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, modelId);
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

}
